package top.iridescent.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import top.iridescent.db.annotation.DataSource;
import top.iridescent.dao.model.request.MhRoleQuery;
import top.iridescent.entity.MhDictInfo;
import top.iridescent.entity.MhRole;
import top.iridescent.service.impl.MhDictInfoServiceImpl;
import top.iridescent.service.impl.MhRoleServiceImpl;
import top.iridescent.util.PageUtils;
import top.iridescent.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author
 * @email
 * @date 2021-09-03 17:01:36
 */
@RestController
@Api(tags = "MhRole")
@RequestMapping("mh/mhrole")
@CrossOrigin
//@DataSource("mh_game_local")
public class MhRoleController {
    @Autowired
    private MhRoleServiceImpl service;
    @Autowired
    private MhDictInfoServiceImpl dictInfoService;

    /**
     * 列表
     */
    @ApiOperation(value = "分页检索", notes = "")
    @PostMapping("/list")
    //@RequiresPermissions("mh:mhrole:list")
    public Result findPage(@RequestBody MhRoleQuery param) {
        return service.findPage(PageUtils.getPage(param), param);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "单个检索 ", notes = "")
    @GetMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("mh:mhrole:info")
    public Result getById(@ApiParam(name = "objId", value = "") @PathVariable Long objId) {
        return service.getById(objId);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增", notes = "")
    @PostMapping(value = "", produces = "application/json")
    //@RequiresPermissions("mh:mhrole:save")
    public Result save(@RequestBody MhRole param) {
        //param.setOperId(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId());
        return service.saveInfo(param);
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新", notes = "")
    @PutMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("mh:mhrole:update")
    public Result update(@ApiParam(name = "objId", value = "") @PathVariable Long objId, @RequestBody MhRole param) {
        param.setId(objId);
        //param.setOperId(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId());
        return service.update(param);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除", notes = "")
    @DeleteMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("mh:mhrole:delete")
    public Result deleteById(@ApiParam(name = "objId", value = "") @PathVariable Long objId) {
        //return service.deleteById(objId);
        return Result.ok();
    }

    @ApiOperation(value = "下拉", notes = "")
    @GetMapping(value = "/drop/down", produces = "application/json")
    public Result<List<MhDictInfo>> raceDropDownList() {
        //QueryWrapper<MhDictInfo> q = new QueryWrapper<>();
        //q.lambda().eq(MhDictInfo::getDictType, param.getDictType());
        List<MhDictInfo> list=dictInfoService.list();
        //Map<String,List<MhDictInfo>> map=list.stream().collect(Collectors.toMap(MhDictInfo::getDictType, Function.identity()));
        Map<String,List<MhDictInfo>> map=list.stream().collect(Collectors.groupingBy(MhDictInfo::getDictType));
        return  Result.ok().setData(map);
    }
}
